﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace ChessTest.Chess.Items
{
    [Serializable]
    /// <summary>
    /// 将
    /// </summary>
    public class KingItem : OneStepItem
    {
        public override string DisplayName =>IsBlack?                     "帅": "将";
   
        public override bool CanOriginMove(Chessboard chessboard,int selectX, int selectY)
        {
            //是否是同一直线
            if (selectX != PositionX && selectY != PositionY)
                return false;


            //判断将帅是否相望
            KingItem king = chessboard.GetChessman(selectX, selectY) as KingItem;
            if (king !=null && king.IsBlack !=IsBlack)
            {
                int minY;
                int maxY;
                if (king.PositionY > PositionY)
                {
                    minY = PositionY;
                    maxY = king.PositionY;
                }
                else
                {
                    minY = king.PositionY;
                    maxY = PositionY;
                }
                int y = minY + 1;
                while (y < maxY)
                {
                    Chessman item = chessboard.GetChessman(selectX, y);
                    if (item != null)
                        break;
                    y++;
                }
                if (y >= maxY)
                    return true;
            }

            if (!base.CanOriginMove(chessboard,selectX, selectY)) return false;

                return true;
        }

        protected override bool IsOverstep(Chessboard chessboard,int x,int y)
        {
            //是否越界
            if (x > 5 || x < 3)
                return true;

            if (PositionY < 3 && (y >= 3 || y<0))
                return true;
            if (PositionY > 6 && (y <= 6 || y>= chessboard.MaxY))
                return true;
            return false;
        }




        public override List<Point> GetOriginCanMoveList(Chessboard chessboard)
        {
            List<Point> list = new List<Point>();

            if (CanOriginMove(chessboard,PositionX + 1, PositionY))
                list.Add(new Point(PositionX + 1, PositionY));

            if (CanOriginMove(chessboard, PositionX - 1, PositionY))
                list.Add(new Point(PositionX - 1, PositionY));
            if (CanOriginMove(chessboard, PositionX , PositionY+1))
                list.Add(new Point(PositionX , PositionY + 1));
            if (CanOriginMove(chessboard, PositionX , PositionY-1))
                list.Add(new Point(PositionX , PositionY-1));

            if (CanOriginMove(chessboard,chessboard.RedKing.PositionX, chessboard.RedKing.PositionY ))
                list.Add(new Point(chessboard.RedKing.PositionX, chessboard.RedKing.PositionY));

            if (CanOriginMove(chessboard,chessboard.BlackKing.PositionX, chessboard.BlackKing.PositionY))
                list.Add(new Point(chessboard.BlackKing.PositionX, chessboard.BlackKing.PositionY));

            return list;
        }



        public override int Score => 100;

    }
}
